package com.nuc.thread.cas;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 诗酒趁年华
 * @version 1.0.0
 * @Description 账户接口
 * @createTime 2021年07月11日 17:56:00
 */
public interface Account {
    /**
     * 获取余额
     * @return
     */
    Integer getBalance();

    /**
     * 取钱
     * @param balance
     */
    void withDraw(Integer balance);

    /**
     * 启动1000个线程，每个线程取10块钱
     * @param account
     */
    static void demo(Account account){
        List<Thread> threadList= new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            Thread thread = new Thread(()->{
                account.withDraw(10);
            });
            threadList.add(thread);
        }
        long start = System.nanoTime();
        threadList.forEach(Thread::start);//启动线程
        threadList.forEach(t->{
            try {
                t.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        long end = System.nanoTime();
        System.out.println(account.getBalance()+"花费了"+(end-start)/1000_000+"ms");

    }
}
